package cn.damong.response;

import java.io.Serializable;

/**
 * 普通操作接口返回给前端页面的数据对象
 * 
 * @author CodingPanda
 * @2019年5月15日 下午10:30:13
 *
 * @param <T>
 */
public class ApiResponse<T> implements Serializable {

    private static final long serialVersionUID = 1335156549735113676L;

    /**
	 * 错误代码，成功时设置为1，失败时按照规范生成相应的错误代码
	 */
	protected String code;
	
	/**
	 * 当code不为1时，错误提示信息
	 */
	protected String message;
	
	/**
	 * 返回的数据
	 */
	private T data;
	
	public ApiResponse(){
		
	}
	
	public ApiResponse(ErrorCode code){
		this.code = code.getValue();
		this.message = code.getMessage();
	}
	
	public ApiResponse(ErrorCode code, T data){
		this.code = code.getValue();
		this.message = code.getMessage();
		this.data = data;
	}

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @param code the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the data
	 */
	public T getData() {
		return data;
	}

	/**
	 * @param data the data to set
	 */
	public void setData(T data) {
		this.data = data;
	}

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((code == null) ? 0 : code.hashCode());
        result = prime * result + ((data == null) ? 0 : data.hashCode());
        result = prime * result + ((message == null) ? 0 : message.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        ApiResponse other = (ApiResponse) obj;
        if (code == null) {
            if (other.code != null) {
                return false;
            }
        } else if (!code.equals(other.code)) {
            return false;
        }
        if (data == null) {
            if (other.data != null) {
                return false;
            }
        } else if (!data.equals(other.data)) {
            return false;
        }
        if (message == null) {
            if (other.message != null) {
                return false;
            }
        } else if (!message.equals(other.message)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "ApiResponse [code=" + code + ", message=" + message + ", data=" + data + "]";
    }
	

}
